<?php

/**
 * 客户投诉 ajax类
 * @package  ajax
 * @subpackage  backend
 * @create by allen.qiang
 */
header('Content-Type:text/html;charset=utf-8');
require 'module/base/customerHandler.class.php';

class ajax_complaint {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //投诉列表
                    $this->complaintList();
                    break;

                case 2: //新增或编辑投诉
                    $this->complaintAdd();
                    break;

                case 3: //删除投诉
                    $this->complaintDel();
                    break;

                case 4: //校验用户是否存在
                    $this->checkCust();
                    break;

                case 5: //检查订单信息
                    $this->checkBill();
                    break;

            }

        }

    }

    //获取用户投诉列表
    public function complaintList(){

        global $G_PGLOB;

        //查询条件
        $customerHandler = new customerHandler( $G_PGLOB->DBCA ) ;
        $result_data	 = $customerHandler->searchCustComplaint( $_REQUEST ) ;

        //查询结果
        $result_array = array(
            'page'  => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows'  => array()
        );

        if ($result_data) {
            foreach($result_data as &$complaint) {

                $result_array['rows'][] = array(
                    'id' 	=> $complaint['complaintID'],
                    'cell' 	=> array(
                       '<a href="javascript: void(0);" onclick="complaintList.edit(' . $complaint['complaintID'] . ')">修改</a>&nbsp;'.
                       '<a href="javascript: void(0);" onclick="complaintList.detail(' . $complaint['complaintID'] . ')">详情</a>&nbsp;',
                        $complaint['complaintID'] ,
                        $complaint['mobile'],
                        $complaint['createTime'],
                        $complaint['complaintContent'],
                        $complaint['memo']
                    )
                );
            }
        }

        echo json_encode( $result_array );

    }

    /**
     * 更新或保存
     */
    private function complaintAdd() {

        global $G_PGLOB;

        //调用卡管理类中的删除代金卡申领单方法
        $customerHandler  = new customerHandler( $G_PGLOB->DBCA ) ;

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;

        $result_array = $customerHandler->addCustComplaint( $_REQUEST ) ;

        echo json_encode($result_array);

    }

    /**
     * 删除客户投诉信息
     *
     */
    private function complaintDel () {

        global $G_PGLOB;

        /**接收参数
         *
         * $_REQUEST[delArr] => Array(
        [0] => 19
        [1] => 18
        [2] => 17
        )
         *这是由要删除的complaintID组成的数组
         */
        $delArr = $_REQUEST['delArr'];

        if( empty($delArr) ){

            echo json_encode( array('code'=>400 , 'message'=>'请选择要删除的信息') );
            return ;

        }

        //统计不存在的信息的数目
        $errorNum = 0;

        //记录要删除的entity对象
        $deleteObjArr = array();

        //检查要删除的信息是否全部存在
        foreach( $delArr as $complaintID ){

            //检查信息是否存在
            $check_obj = new entity('cust_complaint');
            $check_obj->complaintID = $complaintID;

            if( !$G_PGLOB->DBCA->fetch( $check_obj ) )
                $errorNum++;

            $deleteObjArr[] = $check_obj;

        }

        if( $errorNum > 0 ){

            echo json_encode( array('message'=>'所选部分信息已不存在，请刷新页面' , 'code'=>400 ) );
            return;

        }

        try{

            //开启事务
            $G_PGLOB->DBCA->beginTransaction();

            //循环删除
            foreach( $deleteObjArr as $delobj ){

                $G_PGLOB->DBCA->delete( $delobj );

            }

            //提交事务
            $G_PGLOB->DBCA->commit();

            echo json_encode( array('message'=>'删除成功', 'code'=>200) );
            return;

        }catch( Exception $e ){

            //回滚事务
            $G_PGLOB->DBCA->rollBack();

            error_log($e->getMessage());
            echo json_encode( array('message'=>'系统错误，请与管理员联系', 'code'=>500) );
            return;

        }

    }

    //校验用户是否存在
    function checkCust(){

        global $G_PGLOB;

        //实例化会员类
        $customerHandler = new customerHandler( $G_PGLOB->DBCA );

        //调用查询会员方法
        $custInfo = $customerHandler->getCustomerInfo( $_REQUEST );

        if( empty( $custInfo ) ){
            echo json_encode( array( 'code'=>404 , 'data'=>$_REQUEST['custID'] , 'focus'=>'custID' ) );
        }else{
            echo json_encode( array( 'code'=>200 , 'message'=>'ok' ) );
        }

    }

    //校验订单信息
    function checkBill(){

        global $G_PGLOB;

        //从订单主表查询订单信息
        $search_bill_sql = 'SELECT * FROM bill_master WHERE bill_no = ?';
        $billInfo        = $G_PGLOB->DBCA->stmt_query( $search_bill_sql , 's' ,  $_REQUEST['bill_no']);

        if( empty( $billInfo ) ){
            echo json_encode( array( 'code'=>404 , 'data'=>$_REQUEST['bill_no'] , 'focus'=>'bill_no' ) );
        }else{
            echo json_encode( array( 'code'=>200 , 'message'=>'ok' ) );
        }

    }

}